//
//  MovieMoreViewController.m
//  SouhuTV
//
//  Created by 苗旭萌 on 15/6/14.
//  Copyright (c) 2015年 Mike. All rights reserved.
//

#import "MovieMoreViewController.h"
#import "MovieBaseInfo.h"
#import "HomePageCollectionCell.h"
#import "MovieDetailViewController.h"
#import "HistoryUserDefault.h"

@interface MovieMoreViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) UICollectionView* collectionView;

@end

@implementation MovieMoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((self.view.frame.size.width - 2) / 3 , self.view.frame.size.width / 3 * 1.4 + 25);
    layout.minimumInteritemSpacing = 1;
    layout.minimumLineSpacing = 0;
    
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) collectionViewLayout:layout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    
    [self.view addSubview:_collectionView];
    
    [_collectionView registerClass:[HomePageCollectionCell class] forCellWithReuseIdentifier:@"CELL"];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.movieArray.count;
}

- (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HomePageCollectionCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CELL" forIndexPath:indexPath];
    cell.baseInfo = [MovieBaseInfo movieBaseInfoWithDictionary:self.movieArray[indexPath.item]];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    MovieDetailViewController *detail = [[MovieDetailViewController alloc] init];
    MovieBaseInfo* base = [MovieBaseInfo movieBaseInfoWithDictionary:self.movieArray[indexPath.item]];
    [HistoryUserDefault addMovieBaseInfoInUseDefault:base];
    
    
    detail.ID = base.ID;
    [self.navigationController pushViewController:detail animated:YES];
    [detail release];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
